package com.example.todolist.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.todolist.dto.business.WriteCommentsDTO;
import com.example.todolist.mapper.DynamicCommentsMapper;
import com.example.todolist.mapper.DynamicMapper;
import com.example.todolist.model.DynamicComments;
import com.example.todolist.response.BaseResponse;
import com.example.todolist.response.SystemResponse;
import com.example.todolist.service.DynamicCommentsService;
import com.example.todolist.util.HttpServletRequestUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-02-25
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DynamicCommentsServiceImpl extends ServiceImpl<DynamicCommentsMapper, DynamicComments> implements DynamicCommentsService {

    private final DynamicMapper dynamicMapper;

    @Override
    public BaseResponse writeComments(WriteCommentsDTO writeCommentsDto) {
        HttpServletRequest request = HttpServletRequestUtil.getHttpServletRequest();
        String uid = (String) request.getAttribute("uid");
        String nickName = (String) request.getAttribute("nick_name");
        DynamicComments dynamicComments = DynamicComments
                .builder()
                .commentId(IdUtil.simpleUUID())
                .commenterId(uid)
                .commenterNickname(nickName)
                .build();
        BeanUtils.copyProperties(writeCommentsDto, dynamicComments);
        int num = this.baseMapper.insert(dynamicComments);
        if (num != 0) {
            int i = dynamicMapper.updateCommentTotalNum(writeCommentsDto.getDynamicId());
            if (i == 1) {
                return SystemResponse.success();
            } else {
                return SystemResponse.fail("发生异常");
            }
        } else {
            return SystemResponse.fail("发生异常");
        }
    }

    @Override
    public BaseResponse whetherCanDeleteThisComments(String commentId) {
        HttpServletRequest request = HttpServletRequestUtil.getHttpServletRequest();
        String uid = (String) request.getAttribute("uid");
        QueryWrapper<DynamicComments> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_id", commentId);
        DynamicComments comments = this.baseMapper.selectOne(queryWrapper);
        if (comments.getCommenterId().equals(uid)) {
            comments.setCommentCanDelete(1);
        }
        return SystemResponse.success(comments);
    }

    @Override
    public BaseResponse deleteComments(String commentId, String dynamicId) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("comment_id", commentId);
        int i = this.baseMapper.deleteByMap(map);
        if (i == 1) {
            int num = dynamicMapper.updateCommentDeleteTotalNum(dynamicId);
            if (num == 1) {
                return SystemResponse.success();
            } else {
                return SystemResponse.fail("删除失败");
            }
        }
        return SystemResponse.fail("删除失败");

    }
}
